<?php
/**
 * UserController.php
 * 
 * @category    Madoqua
 * @package     Module
 * @subpackage  BlogAdmin
 */

/** base class */
require_once 'Madoqua/Controller/Action/Abstract.php';

/**
 * Admin_UserController
 * 
 * @category    Madoqua
 * @package     Module
 * @subpackage  BlogAdmin
 */
class BlogAdmin_UserController extends Madoqua_Controller_Action_Abstract 
{
    
    /**
     * display/modify the current user's info
     */
    public function indexAction() 
    {
        $form = $this->_formLoader->get('BlogAdmin_User_User');
        $this->view->form = $form;
        //user form
        
        $form->populate((array) $this->_user);
        //populate with current user
        
        $userModel = $this->_modelLoader->get('BlogAdmin_User');
        //user model
        $user = $userModel->fetchById($this->_user->id);
        //the current user as a db table row        
        
        if ($form->isPrg(null, true) && $form->isValid($form->getPrgData())) {
            //woo!

            
            $user->setFromArray($form->getModelValues($userModel));
            //update
            
            $success = true;
        }
        
        $passwordForm = $this->_formLoader->get('BlogAdmin_User_Password');
        $this->view->passwordForm = $passwordForm;
        //password form
        
        if ($passwordForm->isPrg(null, true) && $passwordForm->isValid($form->getPrgData())) {
            //password form has been submitted succesfully
            $user->password = md5($passwordForm->getValue('new_password'));
            //set the new password
            $success = true;
        }
        
        if (!empty($success)) {
            //one of the forms was succesfully submitted
            
            $user = (object) $user->toArray();
            //parse user into stdClass obj (for better session saving)
            Zend_Auth::getInstance()->getStorage()->write($user);
            //set the new user info in Zend_Auth's instance
            
            $this->getHelper('redirector')->gotoRoute(array(
                'controller' => 'user'
            ), 'blog_admin', 'true');
            //redirect back to this action without prg param
        }
    }
}
